package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Advertising;
import com.weipai.springboot.model.Help;
import com.weipai.springboot.model.HelpCate;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.HelpDao;
import com.weipai.springboot.module.service.HelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2019/01/10 16:30
 */
@Service
public class HelpServiceImpl implements HelpService {
    
    @Autowired
    private HelpDao helpDao;


    @Override
    public Response helpCateSave(HelpCate helpCate) {
        Response response = new Response();
        if(helpCate.getId()!=null){
            helpDao.updateByPrimaryKeySelective(helpCate);
        }else{
            helpCate.setStatus(0);
            helpDao.insert(helpCate);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response helpCateList() {
        Response response = new Response();
        List<HelpCate> list = helpDao.list();
        if(!list.isEmpty()){
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", list);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response helpCateChangeStatus(String id) {
        Response response = new Response();
        HelpCate helpCate = helpDao.selectByPrimaryKey(id);
        if(helpCate!=null){
            if(helpCate.getStatus()==0){
                helpCate.setStatus(-1);
            }else{
                helpCate.setStatus(0);
            }
            helpDao.updateByPrimaryKeySelective(helpCate);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response helpCateDelete(String id) {
        Response response = new Response();
        
        List<Map<String, Object>> list = helpDao.getHelpByCateId(id);
        if(!list.isEmpty()){
            response.setCode(RetCode.ASSOCIATED_DATA);
            return response;
        }
        helpDao.deleteByPrimaryKey(id);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response helpSave(Help help) {
        Response response = new Response();
        if(help.getId()!=null){
            helpDao.updateByPrimaryKeySelectiveHelp(help);
        }else{
            help.setStatus(0);
            helpDao.insertHelp(help);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response helpList(Integer page, Integer size, String draw, String type) {
        Integer start = (page-1) * size;
        List<Map<String, Object>> list = helpDao.listHelp(start, size, type);
        Integer totalNum = helpDao.amountHelp(type);
        PageResult<Map<String, Object>> pageList = new PageResult<>(list,page,size,totalNum, draw);
        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    @Override
    public Response helpChangeStatus(String id) {
        Response response = new Response();
        Help help = helpDao.selectByPrimaryKeyHelp(id);
        if(help!=null){
            if(help.getStatus()==0){
                help.setStatus(-1);
            }else{
                help.setStatus(0);
            }
            helpDao.updateByPrimaryKeySelectiveHelp(help);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response helpDelete(String id) {
        Response response = new Response();
        helpDao.deleteByPrimaryKeyHelp(id);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response getInfoById(String id) {
        Response response = new Response();
        Help help = helpDao.selectByPrimaryKeyHelp(id);
        if(help!=null){
            response.setData(help);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }
}
